package cn.edu.cug.cs.gtl.docsrv.auth.controller;

import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.docsrv.dict.exception.BaseException;
import cn.edu.cug.cs.gtl.docsrv.auth.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/auth/login")
public class LoginController {
    @Autowired
    LoginService loginService;

    @GetMapping("/check")
    public String check(@RequestParam(value = "userName", defaultValue = "admin") String userName,
                        @RequestParam(value = "password", defaultValue = "admin") String password) {
        try {
            boolean f = loginService.checkUser(userName, password);
            Configuration.getMasterSession().close();
            if (f) {
                return "true";
            } else {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

     @GetMapping("/filters")
    public List<String> getFilters(@RequestParam(value = "userName", defaultValue = "admin") String userName) {
        try {
            return Configuration.getAuthority().getFilters();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }





//    @GetMapping("/login")
//    public String login(@RequestParam(value = "userName") String userName,
//                        @RequestParam("password") String password,
//                        Model model,
//                        HttpSession session)  {
//        try {
//            boolean f = loginService.checkUser(userName,password);
//            if (f){
//                session.setAttribute("loginUser", userName);
//                session.setMaxInactiveInterval(1800);
//                return "redirect:index.html";
//            }else {
//                model.addAttribute("msg","用户名或者密码错误！");
//                model.addAttribute("username",userName);
//                return "login";
//            }
//        } catch (Exception e) {
//            //异常处理，前端拦截并提示用户，比如无法连接数据库等异常
//            throw new BaseException(e.getMessage());
//        }
//    }
}
